/**
 * FileName: motorcade-personnel-management
 * Author:   郭宗豪
 * Date:     2021/12/12 19:10
 * Version:  v_0.0.1
 * Description:
 */
import AddDialog from './add-dialog2/add-dialog'

import {
  getAllDepartment_,
  getAllRole_,
  getAllDepartmentTreeData,
  getTeamPersonnel,
  getEmployeeInfoByAccount,
  updateEmployee
} from "../../../api/data";
export default {
  name: "motorcade-personnel-management",
  components: {
    AddDialog
  },
  data() {
    return {
      query: {
        department: '1001',
        employeeName: '',
        status: '1',
        page: '1',
        limit: '10',
      },
      states: [
        {
          value: '1',
          label: '正常'
        },
        {
          value: '0',
          label: '停用'
        }],
      isCollapsed: false,
      data6: [
      ],
      columns12: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '账号',
          key: 'account'
        },
        {
          title: '姓名',
          key: 'name'
        },
        {
          title: '身份证号',
          key: 'idCard'
        },
        {
          title: '职位',
          key: 'uroleName'
        },
        {
          title: '部门',
          key: 'corganName'
        },
        {
          title: '电话',
          key: 'phone'
        },
        {
          title: '邮箱',
          key: 'email'
        },
        {
          title: '状态',
          key: 'status'

        },
        {
          title: 'Action',
          slot: 'action',
          width: 200,
          align: 'center'
        }
      ],
      data7: [

      ],
      contextData: null,
      modal1: false,
      formItem: {
        name: '',
        account: '',
        uroleId: 0,
        pwd: '',
        idCard: '',
        sex: 1,
        phone: '',
        email: '',
        birth: '',
        status: 1,
        corganId: 0,
      },
      organizations:[
      ],
      roles:[
      ],
    }
  },
  computed: {},
  methods: {
    changePage(pageNumber){
        this.query.page=pageNumber
        this.queryList()
    },
    changeSize(pageSize){
        this.query.limit=pageSize
    },
    changeDepartment(data) {
      this.contextData = data;
      this.query.department=this.contextData[0].id;
    },
    queryList() {
      getTeamPersonnel(this.query).then(res=>{
        this.data7=res.data.obj;
        this.data7.forEach(
          item=>{
            if(item.status==0){
              item.status="停用"
            }else{
              item.status="正常"
            }
          }
        )
      }).catch(err=>{
        alert(err.data.message)
      })
    },
    queryDepartmentStructure(){
      getAllDepartmentTreeData().then(res=>{
        this.data6=res.data.obj;
      }).catch(err=>{
        alert(err.data.message)
      })
    },
    reset() {
      this.query.employeeName = ''
      this.query.status = 1
    },
    editItem(index){
      getEmployeeInfoByAccount(this.data7[index].account).then(res=>{
        console.log(res.data.obj)
        this.formItem.account=res.data.obj.account;
        this.formItem.name=res.data.obj.name;
        this.formItem.sex=res.data.obj.sex.toString();
        this.formItem.pwd=res.data.obj.pwd;
        this.formItem.idCard=res.data.obj.idCard;
        this.formItem.phone=res.data.obj.phone;
        this.formItem.email=res.data.obj.email;
        this.formItem.birth=res.data.obj.birth;
        this.formItem.status=res.data.obj.status.toString();
        this.formItem.uroleId=res.data.obj.uroleId;
        this.organizations.forEach(organization=>{
          if (organization.corganName==res.data.obj.corganName){
            this.formItem.corganId=organization.corganId;
          }
        })
        this.modal1=true;
      }).catch(err=>{

      })
    },
    ok() {
      this.$Message.info('Clicked ok');
    },
    cancel() {
      this.$Message.info('Clicked cancel');
    },
    getAllDepartment(){
      getAllDepartment_().then(res=>{
        this.organizations=res.data.obj;
      }).catch(err=>{
        alert(err.data.message)
      })
    },
    getAllRole(){
      getAllRole_().then(res=>{
        this.roles=res.data.obj;
      }).catch(err=>{
        alert(err.data.message)
      })
    },
    submit(){
      updateEmployee(this.formItem).then(res=>{
        if (res.data.code==500){
          this.$Message.info(res.data.message);
        }else{
          this.$Message.info("添加成功");
          this.modal1=false;
          this.queryList()
        }
      }).catch(err=>{
      })
    },
    resetForm(){
      this.formItem={
        name: '',
        account: '',
        uroleId: 0,
        password: '',
        numberId: '',
        sex: 1,
        phone: '',
        email: '',
        birth: '',
        status: 1,
        corganId: 0,
      }
    },
  },
  mounted() {
    this.queryDepartmentStructure()
    this.getAllDepartment()
    this.getAllRole()
  }
}
